import 'package:bigegg/global/user/xmm_account_manager.dart';
import 'package:bigegg/global/xmm_app_const.dart';
import 'package:bigegg/utils/xmm_event_bus.dart';
import 'package:bigegg/utils/xmm_toast_util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../../common/get/controller/xmm_base_controller.dart';
import '../../../../../data/repository/xmm_remote_repository.dart';
import '../../../../../entity/user/xmm_block_user.dart';

class XmmBlockListController extends XmmBaseGetxController {
  var remoteRepository = XmmRemoteRepository();

  final blockList = <XmmBlockUser>[].obs;

  @override
  void onReady() {
    super.onReady();
    XmmEventBus.instance.on(XmmAppConst.eventBlockStatusUpdateKey, (arg) => getBlockedList());
  }

  @override
  void onClose() {
    XmmEventBus.instance.off(XmmAppConst.eventBlockStatusUpdateKey);
    super.onClose();
  }

  Future<void> getBlockedList() async {
    var list = await remoteRepository.getBlackList();
    blockList.value = list;
    blockList.refresh();
    XmmAccountManager.instance.updateUserRelation(blockList: list);
  }

  Future<void> unblock(XmmBlockUser user) async {
    try{
      EasyLoading.show();
      await XmmAccountManager.instance.unblock(user.broadcasterId ?? "");
      XmmToastUtils.show("Unblock Successfully");
    } finally {
      EasyLoading.dismiss();
    }
  }

  Future<void> refreshData() async {
    await getBlockedList();
  }
}
